/*
 * Niiuzu
 * Copyright (C) 2011  Thierry Nowak
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.niiuzu.client.screen.feed;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.LazyPresenter;
import com.niiuzu.client.eventbus.FeedEventBus;
import com.niiuzu.client.util.ClientContextFactory;
import com.niiuzu.client.util.LoadingTicket;
import com.niiuzu.client.util.SessionClient;
import com.niiuzu.client.util.callback.AbstractCallback;
import com.niiuzu.shared.model.FeedChannel;
import com.niiuzu.shared.model.FeedEntry;
import com.niiuzu.shared.rpc.FeedServiceAsync;

/**
 * Presenter dédié à la gestion des flux et des données RSS.
 */
@Presenter(view = FeedViewImpl.class)
public class FeedPresenter extends LazyPresenter<FeedView, FeedEventBus> {

	@Inject
	private FeedServiceAsync feedService;

	private ClickHandler refreshClickHandler;

	private LoadingTicket loadingPresenterTicket;

	@Override
	public void bindView() {
		super.bindView();

		refreshClickHandler = new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				sendRefreshFeedDataListRequest();
			}
		};
		view.getRefreshHasClickHandlers().addClickHandler(refreshClickHandler);
	}

	/**
	 * Le chargement du module des flux commence.
	 * 
	 * @see FeedEventBus#feedMainViewLoadStarting(LoadingTicket)
	 */
	public void onFeedMainViewLoadStarting(LoadingTicket ticket) {
		loadingPresenterTicket = ticket;

		eventBus.loadingStarted(ticket);
		eventBus.channelFilterLoadingStarted();
	}

	/**
	 * Fin du chargement de la vue de la liste des flux.
	 * 
	 * @see FeedEventBus#channelFilterLoaded(IsWidget)
	 */
	public void onChannelFilterLoaded(IsWidget widget) {
		view.setChannelFilterView(widget);
		eventBus.feedActionsLoadingStarted();
	}

	/**
	 * Fin du chargement de la vue des action sur les flux.
	 */
	public void onFeedActionsLoaded(IsWidget feedActionsView) {
		view.setFeedActionsView(feedActionsView);
		eventBus.feedViewerLoadingStarted();
	}

	/**
	 * Fin du chargement de la consultation des données provenant des flux RSS.
	 * 
	 * @see FeedEventBus#feedViewerLoaded(IsWidget)
	 */
	public void onFeedViewerLoaded(IsWidget widget) {
		view.setFeedViewerView(widget);

		FeedChannel[] channels = SessionClient.getInstance().getChannelArray();
		feedService.recoverFeedEntries(channels, ClientContextFactory.get(),
				new AbstractCallback<FeedEntry[]>(eventBus) {
					@Override
					public void onSuccess(FeedEntry[] result) {
						recoverFeedEntriesForFirstTime(result);
					}
				});
	}

	/**
	 * Comportement déclenché suite à un clic sur le bouton de rafraîchissement
	 * de la liste.
	 */
	ClickHandler getRefreshClickHandler() {
		return refreshClickHandler;
	}

	/**
	 * Envoie une demande de rafraîchissement de la liste des données.
	 */
	private void sendRefreshFeedDataListRequest() {
		SessionClient sessionClient = SessionClient.getInstance();
		FeedChannel[] channels = sessionClient.getChannelArray();

		view.disableActionComponents();
		view.showRefreshMessage();
		feedService.recoverFeedEntries(channels, ClientContextFactory.get(),
				new AbstractCallback<FeedEntry[]>(eventBus) {
					@Override
					public void onSuccess(FeedEntry[] result) {
						recoverFeedEntries(result);
					}
				});
	}

	/**
	 * Récupère les données suite au premier chargement.
	 */
	void recoverFeedEntriesForFirstTime(FeedEntry[] feedEntries) {
		eventBus.feedEntriesReceived(feedEntries);
		eventBus.feedMainViewLoaded(view, loadingPresenterTicket);
	}

	/**
	 * Récupère les données suite à un rafraîchissement de la liste.
	 */
	void recoverFeedEntries(FeedEntry[] feedEntries) {
		eventBus.feedEntriesReceived(feedEntries);
		view.enableActionComponents();
		view.hideRefreshMessage();
	}

	/**
	 * Réception d'un nouveau flux ajouté dans la session.
	 * 
	 * @see FeedEventBus#channelListUpdated()
	 */
	public void onChannelListUpdated() {
		sendRefreshFeedDataListRequest();
	}

	/* ********************************************************************** */
	/* ********************************************************************** */
	/* ********************************************************************** */

	/**
	 * JUnit - Définit un mock pour {@link FeedServiceAsync}.
	 */
	void setMockFeedService(FeedServiceAsync service) {
		this.feedService = service;
	}
}
